/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.configuration.beans;

import edu.soc.cs5226.gui.dashboard.chart.beans.*;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Huimin
 */
@ManagedBean
@RequestScoped
public class Configuration {
    
    private static final String CONFIG_PAGE = "configPage.xhtml";
    private ConfigurationDAO openConfigDAO;
    private Configuration retrieveConfig;

    private long bc_redThreshold;
    private long bc_yellowThreshold;
    private long lb_redThreshold;
    private long lb_yellowThreshold;
    private long s_redThreshold;
    private long s_yellowThreshold;
    private long rs_redThreshold;
    private long rs_yellowThreshold;
    
    private long sps_redThreshold;
    private long sps_yellowThreshold;
    private long XValue;  
    private long YValue;
    
    public Configuration() {
        openConfigDAO = new ConfigurationDAO();
    }
    
    public Configuration (long bc_redThreshold, long bc_yellowThreshold, long lb_redThreshold, long lb_yellowThreshold,
            long s_redThreshold, long s_yellowThreshold, long rs_redThreshold, long rs_yellowThreshold, long sps_redThreshold, long sps_yellowThreshold,
            long XValue, long YValue)
    {
        this.bc_redThreshold = bc_redThreshold;
        this.bc_yellowThreshold = bc_yellowThreshold;
        this.lb_redThreshold = lb_redThreshold;
        this.lb_yellowThreshold = lb_yellowThreshold;
        this.s_redThreshold = s_redThreshold;
        this.s_yellowThreshold = s_yellowThreshold;
        this.rs_redThreshold = rs_redThreshold;
        this.rs_yellowThreshold = rs_yellowThreshold;
        this.sps_redThreshold = sps_redThreshold;
        this.sps_yellowThreshold = sps_yellowThreshold;
        this.XValue = XValue;
        this.YValue = YValue;
    }
    
    public Configuration getRetrieveConfig() {
        return retrieveConfig;
    }

    public void setRetrieveConfig(Configuration retrieveConfig) {
        this.retrieveConfig = retrieveConfig;
    }
    
    public long getXValue() {
        return XValue;
    }

    public void setXValue(long XValue) {
        this.XValue = XValue;
    }

    public long getYValue() {
        return YValue;
    }

    public void setYValue(long YValue) {
        this.YValue = YValue;
    }

    public long getBc_redThreshold() {
        return bc_redThreshold;
    }

    public void setBc_redThreshold(long bc_redThreshold) {
        this.bc_redThreshold = bc_redThreshold;
    }

    public long getBc_yellowThreshold() {
        return bc_yellowThreshold;
    }

    public void setBc_yellowThreshold(long bc_yellowThreshold) {
        this.bc_yellowThreshold = bc_yellowThreshold;
    }

    public long getLb_redThreshold() {
        return lb_redThreshold;
    }

    public void setLb_redThreshold(long lb_redThreshold) {
        this.lb_redThreshold = lb_redThreshold;
    }

    public long getLb_yellowThreshold() {
        return lb_yellowThreshold;
    }

    public void setLb_yellowThreshold(long lb_yellowThreshold) {
        this.lb_yellowThreshold = lb_yellowThreshold;
    }

    public long getRs_redThreshold() {
        return rs_redThreshold;
    }

    public void setRs_redThreshold(long rs_redThreshold) {
        this.rs_redThreshold = rs_redThreshold;
    }

    public long getRs_yellowThreshold() {
        return rs_yellowThreshold;
    }

    public void setRs_yellowThreshold(long rs_yellowThreshold) {
        this.rs_yellowThreshold = rs_yellowThreshold;
    }

    public long getS_redThreshold() {
        return s_redThreshold;
    }

    public void setS_redThreshold(long s_redThreshold) {
        this.s_redThreshold = s_redThreshold;
    }

    public long getS_yellowThreshold() {
        return s_yellowThreshold;
    }

    public void setS_yellowThreshold(long s_yellowThreshold) {
        this.s_yellowThreshold = s_yellowThreshold;
    }

    public long getSps_redThreshold() {
        return sps_redThreshold;
    }

    public void setSps_redThreshold(long sps_redThreshold) {
        this.sps_redThreshold = sps_redThreshold;
    }

    public long getSps_yellowThreshold() {
        return sps_yellowThreshold;
    }

    public void setSps_yellowThreshold(long sps_yellowThreshold) {
        this.sps_yellowThreshold = sps_yellowThreshold;
    }
    
    public String load_configPage() {
        String pageToGo = CONFIG_PAGE;
        
        try
        {
            retrieveConfig = openConfigDAO.getConfiguration();
            this.bc_redThreshold = retrieveConfig.getBc_redThreshold();
            this.bc_yellowThreshold = retrieveConfig.getBc_yellowThreshold();
            this.lb_redThreshold = retrieveConfig.getLb_redThreshold();
            this.lb_yellowThreshold = retrieveConfig.getLb_yellowThreshold();
            this.s_redThreshold = retrieveConfig.getS_redThreshold();
            this.s_yellowThreshold = retrieveConfig.getS_yellowThreshold();
            this.rs_redThreshold = retrieveConfig.getRs_redThreshold();
            this.rs_yellowThreshold = retrieveConfig.getRs_yellowThreshold();
            this.sps_redThreshold = retrieveConfig.getSps_redThreshold();
            this.sps_yellowThreshold = retrieveConfig.getSps_yellowThreshold();
            this.XValue = retrieveConfig.getXValue();
            this.YValue = retrieveConfig.getYValue();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return pageToGo;
    }
     
    public void saveConfig() {
        String msg;
        try
        {
            Configuration latestConfig = new Configuration(bc_redThreshold, bc_yellowThreshold, lb_redThreshold, lb_yellowThreshold,
            s_redThreshold, s_yellowThreshold, rs_redThreshold, rs_yellowThreshold, sps_redThreshold, sps_yellowThreshold,
            XValue, YValue);
            msg = openConfigDAO.saveConfiguration(latestConfig);
            
            if (msg.equalsIgnoreCase("Successful!")){
                MemoryAreaChartBean.getInstance().displayNewIntervals();
                BufferCacheChartBean.getInstance().displayNewIntervals();
                LogBufferChartBean.getInstance().displayNewIntervals();
                RollbackChartBean.getInstance().displayNewIntervals();
                SharedPoolChartBean.getInstance().displayNewIntervals();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Saved Successfully"));
            }
            else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Saved Unsuccessfully!"));
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }
}
